#include <stdio.h>
#include <stdlib.h>

#include "shp.h"

int reversebytes_uint32t(int value)
{
    return (value & 0x000000FFU) << 24 | (value & 0x0000FF00U) << 8 |
           (value & 0x00FF0000U) >> 8 | (value & 0xFF000000U) >> 24;
}
void read_shp(FILE *shp)
{
    shp_head head;
    fread(&head, sizeof(head), 1, shp);
    head.File_Code = reversebytes_uint32t(head.File_Code);
    // File_Lenth 存放的是16位字 1个字 占 16位 ==》 2字节
    head.File_Lenth = reversebytes_uint32t(head.File_Lenth);
    printf("%d %d\n", head.File_Code, head.File_Lenth);

    printf("x_min:%lf y_min:%lf\nx_max:%lf y_max:%lf\n", head.X_min, head.Y_min, head.X_max, head.Y_max);
    //文件头读取完成
    //1 point 3 lines 5 ploygon

    // 点类型的读取
    switch (head.Shape_Type)
    {
    case 1:
        while (!feof(shp))
        {
            recorde_head r_h;
            if (fread(&r_h, sizeof(recorde_head), 1, shp))
            {
                r_h.recorde_id = reversebytes_uint32t(r_h.recorde_id);
                // 这里也是16位字 point ==> 10 == (4+8+8)/2
                r_h.content_lenth = reversebytes_uint32t(r_h.content_lenth);
                printf("%d %d ", r_h.recorde_id, r_h.content_lenth);
            }
            else
                break;
            point p;
            fread(&p, sizeof(point), 1, shp);
            printf("%lf %lf\n", p.x, p.y);
        }
        break;
    case 3:
        while (!feof(shp))
        {
            recorde_head r_h;
            if (fread(&r_h, sizeof(recorde_head), 1, shp))
            {
                r_h.recorde_id = reversebytes_uint32t(r_h.recorde_id);
                // 这里也是16位字 point ==> 10 == (4+8+8)/2
                r_h.content_lenth = reversebytes_uint32t(r_h.content_lenth);
                printf("%d %d ", r_h.recorde_id, r_h.content_lenth);
            }
            else
                break;
            line l;
            fread(&(l.box), sizeof(double), 4, shp);
            fread(&(l.NumParts), sizeof(int), 1, shp);
            fread(&(l.NumPoints), sizeof(int), 1, shp);
            l.parts = (int *)malloc(sizeof(int) * l.NumParts);
            l.points = (point *)malloc(sizeof(point) * l.NumPoints);

            fread(l.parts, sizeof(int), l.NumParts, shp);
            for (int i = 0; i < l.NumParts; i++)
                printf("%d ", l.parts[i]);

            fread(l.points, sizeof(point), l.NumPoints, shp);
            for (int i = 0; i < l.NumPoints; i++)
                printf("<%.1lf %.1lf> ", l.points[i].x, l.points[i].y);
            printf("END\n");
        }
        break;
    default:
        break;
    }
}

int main()
{
    /*******************code*******************/
    char file_name[100] = "E:/Desktop/my_file/data_struct/11GIS/2.Read_Shape_File/cwz.shp";
    //E:\Desktop\my_file\data_struct\11GIS\2.Read_Shape_File\shp_read.cpp
    FILE *shp;
    if ((shp = fopen(file_name, "r")) == NULL)
    {
        printf("error");
        return 0;
    }

    read_shp(shp);
    /******************************************/
    fflush(stdin);
    printf("\n\n****************************\n");
    printf("Press Enter key to continue\n");
    getchar();
    return 0;
    /******************************************/
}
